#ifndef CH_SORTCLASS_H
#define CH_SORTCLASS_H

#pragma once

/////////////////////////////////////////////////////////////////////////////
// CH_DATA_TYPE - Enumeration used by CHSortClass for specifying
// what type of soring to perform.
//
typedef enum CH_DATA_TYPE
{
	DT_INT = 1,		// sort type int
	DT_STRING,		// sort type string
	DT_DATETIME,	// sort type date / time
	DT_DEC			// sort type decimal
};

/////////////////////////////////////////////////////////////////////////////
// This class will sort a List control by a column of text, integer, float or
// date/time type. It could be easily extended for other data types. To use this
// class, instanciate an CHSortClass object, passing in a pointer to the
// list control as the first argument and the column index as the second arg
// example:
// CHSortClass sortClass(pListCtrl, 0);
// Then call the sort method setting ascending as the first argument either
// TRUE or FALSE, and the type of sort to perform in the second argrument
// example:
// sortClass.Sort(TRUE, DT_STRING);
//
class AFX_EXT_CLASS CHSortClass
{

public:
	
    // Constructs a CHSortClass object.
    //
	CHSortClass(
        // Pointer to a CListCtrl object.
        CListCtrl* pListCtrl,
        // Inex of column to be sorted.
        const int nCol);

	// Destroys a CHSortClass object, handles cleanup and de-allocation.
    //
    virtual ~CHSortClass();

protected:

	CListCtrl* m_pListCtrl;	// pointer to the CListCtrl object to peform sort on

public:

	// This member function is call to peform the actual sort proceedure.
    //
	virtual void Sort(
        // TRUE to sort ascending.
        bool bAsc,
        // Type of sort to perform, can be one of the following:
        //[pre]
        // DT_INT      - Sort type int
        // DT_STRING   - Sort type string
        // DT_DATETIME - Sort type date / time
        // DT_DEC	   - Sort type decimal
        //[/pre]
        CH_DATA_TYPE eType);

protected:

	// This callback member function is called to compare to data items 
	// during sorting operations.
    // The comparison function must return a negative value if the first item should 
    // precede the second, a positive value if the first item should follow the second, 
    // or zero if the two items are equivalent.
    //
	static int CALLBACK Compare(
        // Specify the item data for the two items being compared
        LPARAM lParam1,
        // Specify the item data for the two items being compared
        LPARAM lParam2,
        // Specifies the application-defined value that is passed to the comparison function
        LPARAM lParamSort);
};


#endif //CH_SORTCLASS_H
